package org.example;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class OutputTxt {
	private File file = null;
	public void Init() throws IOException {
		file = new File(System.getProperty("user.dir"));
        file = new File(file.toString() + "/每日新闻");
        if(!file.exists()) {
        	file.mkdirs();
        }
        String DATE_FORMAT = "yyyy年MM月dd日";
        LocalDate localDate = LocalDate.now();
        String result = localDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));
        file = new File(file.toString() + "/"+result+".txt");
        if(!file.exists()) {
        	file.createNewFile();
        }
	}
	public void write(List<NewsBody> list,boolean flag,String title) throws IOException {
		FileWriter fileWriter = new FileWriter(file,flag);
		BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
		bufferedWriter.write(title);
		bufferedWriter.newLine();
		for(int i = 0;i<list.size();i++) {
			NewsBody newsBody = list.get(i);
			bufferedWriter.write(i+1+"."+newsBody.head);
			bufferedWriter.newLine();
			bufferedWriter.write(newsBody.Url);
			bufferedWriter.newLine();
			bufferedWriter.write(newsBody.firstP);
			bufferedWriter.newLine();
			bufferedWriter.newLine();
		}
		bufferedWriter.flush();
		bufferedWriter.close();
		fileWriter.close();
	}
}
